from sklearn.linear_model import LinearRegression
# L2正则（脊回归/岭回归）和L1正则（套索回归）
from sklearn.linear_model import Ridge, Lasso
from sklearn.datasets import load_boston
import numpy as np
import matplotlib.pyplot as plt
from python_ai.common.xcommon import sep

fig = plt.figure(figsize=[15, 5])
spr = 1
spc = 3
spn = 0

# x, y = load_boston(return_X_y=True)
data = load_boston()
x = data.data
y = data.target

# linear regression
sep('lin reg')
spn += 1
plt.subplot(spr, spc, spn)
model = LinearRegression()
model.fit(x, y)
print(model.coef_)
print(model.intercept_)
h = model.predict(x)
idx = np.argsort(y)  # ATTENTION https://stackoverflow.com/questions/50879469/numpy-sort-arrays-based-on-last-column-values
hh = h[idx]
yy = y[idx]
plt.scatter(y, y, c='b', label='target', s=1, zorder=100)
plt.plot(yy, hh, c='r', label='line reg')
plt.legend()

# L2
# 减小所有权重值
sep('L2 lin reg')
spn += 1
plt.subplot(spr, spc, spn)
model = Ridge(alpha=20)
model.fit(x, y)
print(model.coef_)
print(model.intercept_)
h = model.predict(x)
idx = np.argsort(y)
hh = h[idx]
yy = y[idx]
plt.scatter(y, y, c='b', label='target', s=1, zorder=100)
plt.plot(yy, hh, c='r', label='L2')
plt.legend()

# L1
# 筛选特征，生成稀疏矩阵
sep('L1 lin reg')
spn += 1
plt.subplot(spr, spc, spn)
model = Lasso(alpha=0.8)
model.fit(x, y)
print(model.coef_)
print(model.intercept_)
h = model.predict(x)
idx = np.argsort(y)
hh = h[idx]
yy = y[idx]
plt.scatter(y, y, c='b', label='target', s=1, zorder=100)
plt.plot(yy, hh, c='r', label='L1')
plt.legend()

plt.show()
